package com.ms.game.invitefriend.async;

import java.util.ArrayList;

import com.ms.game.invitefriend.InviteCodeProcessor;
import com.ms.game.invitefriend.db.InviteCodeModel;
import com.ms.game.invitefriend.db.InviteFriendDAO;
import com.ms.game.invitefriend.message.InviteCodeData;
import com.ms.game.invitefriend.message.SGetInviteCodeResponse;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class GetInviteCodeDCLAsync extends TaskAsyncCommand {

	/**
	 * 角色ID
	 */
	public Player player;
	
	/**
	 * sessionID
	 */
	public GUID sessionId;
	
	public int messageType;
	@Override
	public void asyncExecute() throws Exception {
		SGetInviteCodeResponse response = new SGetInviteCodeResponse();
		byte identifyType = player.getPlayerIdentityType();
		
		String playerId = player.getGuid().toString();
		
		identifyType = InviteCodeProcessor.playerIdentifyCheck(identifyType, playerId);
	
		ArrayList<InviteCodeModel> inviteCodes = InviteFriendDAO.getInviteCodeModelsByPlayerId(playerId); 
		
		ArrayList<InviteCodeData> playerInviteCodeDatas = new ArrayList<InviteCodeData>();
		int usedCount = 0;
		for (InviteCodeModel inviteCodeModel : inviteCodes) {
			InviteCodeData data = new InviteCodeData();
			data.setInviteCode(inviteCodeModel.getInviteCode());
			data.setUsed(inviteCodeModel.isHaveBeenUsed());
			if(inviteCodeModel.isHaveBeenUsed()){
				usedCount ++;
			}
			playerInviteCodeDatas.add(data);
		}
		
		response.setInviteCodeDatas(playerInviteCodeDatas);
		response.setPlayerType(identifyType);
		response.setTotalNum(inviteCodes.size());
		response.setUsedNum(usedCount);
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionId, messageType, response);
	}
	
	@Override
	public void execute() throws Exception {
		
	}
}
